﻿Public Class ButtonCheck
    Public Enum CheckStyle
        Style1 = 0
        Style2 = 1
        Style3 = 2
        Style4 = 3
        Style5 = 4
        Style6 = 5
    End Enum

    Private Sub ButtonCheck_Click(sender As Object, e As EventArgs) Handles Me.Click
        isCheck = Not isCheck
        Me.Invalidate()
    End Sub

    Public Sub New()

        ' 此调用是设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        '设置Style支持透明背景色并且双缓冲
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.DoubleBuffer, True)
        Me.SetStyle(ControlStyles.ResizeRedraw, True)
        Me.SetStyle(ControlStyles.Selectable, True)
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.BackColor = Color.Transparent

        Me.Cursor = Cursors.Hand
        Me.Size = New Size(87, 27)
    End Sub

    Dim isCheck As Boolean = False
    ''' <summary>
    ''' 是否选中
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Checked As Boolean
        Get
            Return isCheck
        End Get
        Set(value As Boolean)
            isCheck = value
            Me.Invalidate()
        End Set
    End Property

    Dim m_checkStyle As CheckStyle = CheckStyle.Style1
    ''' <summary>
    ''' 样式
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property CheckStyleX As CheckStyle
        Get
            Return m_checkStyle
        End Get
        Set(value As CheckStyle)
            m_checkStyle = value
            Me.Invalidate()
        End Set
    End Property

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim bitMapOn, bitMapOff As Bitmap

        Select Case m_checkStyle
            Case CheckStyle.Style1
                bitMapOn = My.Resources.btncheckon1
                bitMapOff = My.Resources.btncheckoff1
            Case CheckStyle.Style2
                bitMapOn = My.Resources.btncheckon2
                bitMapOff = My.Resources.btncheckoff2
            Case CheckStyle.Style3
                bitMapOn = My.Resources.btncheckon3
                bitMapOff = My.Resources.btncheckoff3
            Case CheckStyle.Style4
                bitMapOn = My.Resources.btncheckon4
                bitMapOff = My.Resources.btncheckoff4
            Case CheckStyle.Style5
                bitMapOn = My.Resources.btncheckon5
                bitMapOff = My.Resources.btncheckoff5
            Case CheckStyle.Style6
                bitMapOn = My.Resources.btncheckon6
                bitMapOff = My.Resources.btncheckoff6
            Case Else
                bitMapOn = My.Resources.btncheckon1
                bitMapOff = My.Resources.btncheckoff1
        End Select

        Dim g = e.Graphics
        Dim rec As New Rectangle(0, 0, Me.Size.Width, Me.Size.Height)

        If isCheck Then
            g.DrawImage(bitMapOn, rec)
        Else
            g.DrawImage(bitMapOff, rec)
        End If
        'MyBase.OnPaint(e)
    End Sub
End Class
